#include "pilat.h"
#include "pilasend.h"
#include "triangulo.h"
#include "nodot.h"
#include <stdio.h>

#include <iostream>
#include <fstream>
using namespace std;
PilaT::PilaT()
{
pila=0;
maxindex=0;
}
void PilaT::add(triangulo* t){
    if(pila==0){pila=new nodoT( t);}
    else{ nodoT* aux=pila;
        while(aux->next!=0){aux=aux->next;}
     aux->next=new nodoT(t);
   //  aux->next->prev=aux;
    maxindex++;

}}
void PilaT::remove(triangulo* t){
    nodoT* aux=pila;
    nodoT* aux2=0;
    if(aux->t==t){
        pila=aux->next;
        free(aux);}
    else{aux2=aux->next;
        while(aux2!=0){
            if(aux2->t==t){
                aux->next=aux2->next;
                free(aux2);
            }
            aux=aux2;
            aux2=aux2->next;
        }}

}

void PilaT::dibujar(){
    nodoT* aux=pila;
    while(aux!=0){
       aux->t->dibujar();
    aux=aux->next;}
}
triangulo* PilaT::getrandom(){


    srand(time(NULL));
    nodoT* aux=pila;
    for(int i=0;i<rand()%maxindex;i++){
        aux=aux->next;
    }
    return aux->t;

}

void  PilaT::crearMalla(int i){
   triangulo* t[4];
  float vertices[5][2];

  vertices[0][0]=0.0+i;
  vertices[0][1]=0.0;

  vertices[1][0]=0.0+i;
  vertices[1][1]=7.0;

  vertices[2][0]=7.0+i;
  vertices[2][1]=7.0;

  vertices[3][0]=7.0+i;
  vertices[3][1]=0.0;

  vertices[4][0]=4.0+i;
  vertices[4][1]=1.0;

  t[0]=new triangulo(vertices[0][0],vertices[0][1],vertices[1][0],vertices[1][1],vertices[4][0],vertices[4][1]);

  t[1]=new triangulo(vertices[1][0],vertices[1][1],vertices[2][0],vertices[2][1],vertices[4][0],vertices[4][1]);

  t[2]=new triangulo(vertices[2][0],vertices[2][1],vertices[3][0],vertices[3][1],vertices[4][0],vertices[4][1]);

  t[3]=new triangulo(vertices[3][0],vertices[3][1],vertices[0][0],vertices[0][1],vertices[4][0],vertices[4][1]);

  t[0]->setVecino(t[1],2);
  t[0]->setVecino(t[3],3);
  t[1]->setVecino(t[2],2);
  t[1]->setVecino(t[0],3);
  t[2]->setVecino(t[3],2);
  t[2]->setVecino(t[1],3);
  t[3]->setVecino(t[0],2);
  t[3]->setVecino(t[2],3);

 add(t[0]);
  add(t[1]);
  add(t[2]);
  add(t[3]);

      //t[0]->lepp(this);
    //for(int i=0;i<100000;i++){
    //triangulo* t1;
    //getrandom()->lepp(this);
//std::cout << "Test\n"<<  t[0]->ladoMayor()<<"eso"<<maxindex;



}

/*int PilaT::countValidos(){
    int i=0;
  for(int j=0;j<maxindex;j++){
      //std::cout << "indice"<<  i  <<"eso\n";
      if(pila[j]->valido==1){
          i++;
      }
  }
      return i;

  }*/

void  PilaT::refinarPedidos(pilaSend* ps,pilaSend* ps2 ){
    float* p=ps->get();
    while(p!=0){
        recibirPunto(ps2,p);
         p=ps->get();
    }
    }

void  PilaT::recibirPunto(pilaSend* ps2,float* p1){
//if esta en el borde
  nodoT* aux=pila;
    while(aux!=0){
       if((aux->t->solveT(p1))>0){
          // printf(" p1 %f %f\n", *p1,*(p1+1));
           aux->t->refinarBypunto(this,ps2,p1);
           return;
       }
    aux=aux->next;}

}
void  PilaT::print(){
//if esta en el borde
  nodoT* aux=pila;
    while(aux!=0){
       printf("trangulo %d\n", aux->t->id);

    aux=aux->next;}

}
void PilaT::printToFile(char* nombre){
  ofstream myfile;
  myfile.open (nombre);

    nodoT* aux=pila;
    while(aux!=0){
      myfile << aux->t->vertices[0][0] <<" "<< aux->t->vertices[0][1]
              <<" "<< aux->t->vertices[1][0] <<" "<< aux->t->vertices[1][1]
              <<" "<< aux->t->vertices[2][0] <<" "<< aux->t->vertices[2][1] <<"\n";
    aux=aux->next;}

  myfile.close();
}
void PilaT::readFromFile(char* nombre){
    ifstream in(nombre);
 float vertices[6];
while (! in.eof() ){

  in >> vertices[0]>> vertices[1]>> vertices[2]>> vertices[3]>> vertices[4]>> vertices[5];
  add(new triangulo(vertices[0], vertices[1], vertices[2], vertices[3], vertices[4], vertices[5]));
}
  in.close();
}

